package kompics.p2p.chord.monitor;


	import java.io.IOException;

	import org.apache.log4j.PropertyConfigurator;
	import org.slf4j.Logger;
	import org.slf4j.LoggerFactory;

	import se.sics.kompics.Component;
	import se.sics.kompics.ComponentDefinition;
	import se.sics.kompics.Kompics;
	import se.sics.kompics.network.Network;
	import se.sics.kompics.network.mina.MinaNetwork;
	import se.sics.kompics.network.mina.MinaNetworkInit;
	import se.sics.kompics.p2p.monitor.chord.server.ChordMonitorConfiguration;
	import se.sics.kompics.p2p.monitor.chord.server.ChordMonitorServer;
	import se.sics.kompics.p2p.monitor.chord.server.ChordMonitorServerInit;
	import se.sics.kompics.timer.Timer;
	import se.sics.kompics.timer.java.JavaTimer;
	import se.sics.kompics.web.Web;
	import se.sics.kompics.web.jetty.JettyWebServer;
	import se.sics.kompics.web.jetty.JettyWebServerConfiguration;
	import se.sics.kompics.web.jetty.JettyWebServerInit;

	/**
	 * The <code>ChordMonitorServerMain</code> class.
	 * 
	 * @author Cosmin Arad <cosmin@sics.se>
	 * @version $Id: ChordMonitorServerMain.java 1295 2009-10-01 12:36:54Z Cosmin $
	 */
	public class ChordMonitorServerMain extends ComponentDefinition {
		static {
			PropertyConfigurator.configureAndWatch("log4j.properties");
		}
		
		/** The Constant logger. */
		private static final Logger logger = LoggerFactory
				.getLogger(ChordMonitorServerMain.class);

		public static void main(String[] args) {
			Kompics.createAndStart(ChordMonitorServerMain.class, 8);
		}

		/**
		 * Instantiates a new chord monitor server main.
		 * 
		 * @throws IOException
		 *             Signals that an I/O exception has occurred.
		 */
		public ChordMonitorServerMain() throws IOException {
			// loading component configurations
			final ChordMonitorConfiguration chordMonitorConfiguration = ChordMonitorConfiguration
					.load("chord_monitor.properties");
			final JettyWebServerConfiguration webConfiguration = JettyWebServerConfiguration
					.load("web_server.properties");

			// creating components
			Component bootstrapServer = create(ChordMonitorServer.class);
			Component timer = create(JavaTimer.class);
			Component network = create(MinaNetwork.class);
			Component web = create(JettyWebServer.class);

			// initializing components
			trigger(new ChordMonitorServerInit(chordMonitorConfiguration),
					bootstrapServer.getControl());
			trigger(new MinaNetworkInit(chordMonitorConfiguration
					.getMonitorServerAddress(), 5), network.getControl());
			trigger(new JettyWebServerInit(webConfiguration), web.getControl());

			// connecting components
			connect(bootstrapServer.getNegative(Timer.class), timer
					.getPositive(Timer.class));
			connect(bootstrapServer.getNegative(Network.class), network
					.getPositive(Network.class));
			connect(bootstrapServer.getPositive(Web.class), web
					.getNegative(Web.class));

			logger.info("Started. Network={} Web={}", chordMonitorConfiguration
					.getMonitorServerAddress(), webConfiguration.getIp()
					.getHostAddress()
					+ ":" + webConfiguration.getPort());
		}
	}